package com.thinkgem.jeesite.modules.ta.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.ta.util.DataBaseUtil;

/**
 * Servlet implementation class ShowImage
 */
public class ShowImage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowImage() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id").toString();
		InputStream in = DataBaseUtil.getImageStreamFromDataBase(id);
		OutputStream toClient = response.getOutputStream();
		response.reset();
		response.setContentType("image/jpg");// 或gif
		int len = 10 * 1024 * 1024;
		byte[] P_Buf = new byte[len];
		int i;
		while ((i = in.read(P_Buf)) != -1) {
			toClient.write(P_Buf, 0, i);
		}
		in.close();
		toClient.flush();
		toClient.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
